package xyz.yuzegod.megawalls.classes.mythic.mole;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.Firework;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.CollectSkill;
import xyz.yuzegod.megawalls.game.ChestManager;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FourthSkill extends CollectSkill
{
    public FourthSkill(final Classes classes) {
        super("囤铁", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 1.0;
            }
            case 2: {
                return 2.0;
            }
            case 3: {
                return 3.0;
            }
            default: {
                return 1.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7找到宝箱时,有§a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率");
            lore.add("   §7额外获得铁锭。");
            return lore;
        }
        lore.add(" §8▪ §7找到宝箱时,有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率");
        lore.add("   §7额外获得铁锭。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill4Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill4Level();
    }
    
    @Override
    public void onBlockBreak(final KitStatsContainer kitStats, final BlockBreakEvent e) {
        final GamePlayer gamePlayer = GamePlayer.get(e.getPlayer().getUniqueId());
        this.getClasses().getSecondSkill().use(gamePlayer, kitStats);
        this.getClasses().getThirdSkill().use(gamePlayer, kitStats);
        if ((ItemUtils.isResource(e.getBlock())) && MegaWalls.getRandom().nextInt(MegaWalls.getInstance().getGame().isWallsFall() ? 1000 : 150) <= 6) {
            e.setCancelled(true);
            final Block block = e.getBlock();
            block.setType(Material.TRAPPED_CHEST);
            gamePlayer.addProtectedBlock(block);
            final Chest chest = (Chest)block.getState();
            block.getWorld().spawn(block.getLocation(), (Class)Firework.class);
            ChestManager.fillInventory(e.getPlayer(), chest.getBlockInventory());
        }
        Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
            @Override
            public void run() {
                if (ItemUtils.matchMaterial(e.getBlock() ,Material.TRAPPED_CHEST) && MegaWalls.getRandom().nextInt(100) <= FourthSkill.this.getAttribute(kitStats.getSkill4Level()) * 100.0) {
                    final Chest chest = (Chest)e.getBlock().getState();
                    chest.getBlockInventory().addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 8) });
                }
            }
        }, 3L);
    }
}
